<template>
  <nav class="navbar navbar-dark bg-primary justify-content-between mb-4 px-4">
    <router-link class="navbar-brand" to="/">者也</router-link>

    <ul v-if="!user.isLogin" class="list-inline mb-0">
      <li class="list-inline-item">
        <router-link to="/login" class="btn btn-outline-light my-2"
          >登陆</router-link
        >
      </li>
      <li class="list-inline-item">
        <router-link to="/signup" class="btn btn-outline-light my-2"
          >注册</router-link
        >
      </li>
    </ul>
    <ul v-else class="list-inline mb-0">
      <DropDown :title="` 您好${user.nickName}`">
        <DropDownItem
          ><router-link to="/create" class="dropdown-item"
            >新建文章</router-link
          ></DropDownItem
        >
        <DropDownItem
          ><router-link :to="`/column/${user.column}`" class="dropdown-item"
            >我的专栏</router-link
          ></DropDownItem
        >

        <DropDownItem
          ><a href="#" class="dropdown-item">编辑资料</a></DropDownItem
        >

        <DropDownItem>
          <a class="dropdown-item" @click.prevent="handleLogout"
            >退出登陆</a
          ></DropDownItem
        >
      </DropDown>
    </ul>
  </nav>
</template>

<script lang="ts">
import createMessage from '../hooks/createMessage'
import { useRouter } from 'vue-router'
import { defineComponent, PropType } from 'vue'
import DropDown from './DropDown.vue'
import DropDownItem from './DropDownItem.vue'
import { UserProps } from '../store/index'
import { useStore } from 'vuex'
export default defineComponent({
  name: 'GlobalHeader',
  components: {
    DropDown,
    DropDownItem
  },
  props: {
    user: {
      required: true,
      type: Object as PropType<UserProps>
    }
  },
  setup() {
    const router = useRouter()
    const store = useStore()
    const handleLogout = () => {
      store.commit('LOGOUT')

      createMessage('退出成功 2s跳转登录页', 'success')
      setTimeout(() => {
        router.push('/login')
      }, 2000)
    }
    return { handleLogout }
  }
})
</script>

<style lang="" scoped></style>
